<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>天气精灵 - 智能天气查询</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --primary: #4A90E2;
            --secondary: #6C5CE7;
            --background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            --text-primary: #2d3436;
            --card-bg: rgba(255, 255, 255, 0.95);
        }

        body {
            font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
            max-width: 800px;
            margin: 2rem auto;
            padding: 0 20px;
            background: var(--background);
            min-height: 100vh;
            color: var(--text-primary);
        }

        .weather-card {
            background: var(--card-bg);
            border-radius: 20px;
            padding: 2rem;
            margin-top: 2rem;
            box-shadow: 0 8px 32px rgba(0,0,0,0.1);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255,255,255,0.3);
            animation: cardEntrance 0.6s ease-out;
        }

        @keyframes cardEntrance {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .form-group {
            display: flex;
            gap: 12px;
            margin-bottom: 1.5rem;
            position: relative;
        }

        input[type="text"] {
            flex: 1;
            padding: 1rem;
            border: 2px solid #e0e0e0;
            border-radius: 12px;
            font-size: 1rem;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            background: rgba(255,255,255,0.9);
        }

        input[type="text"]:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.2);
        }

        button {
            padding: 0.8rem 1.5rem;
            background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
            color: white;
            border: none;
            border-radius: 12px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: 600;
            letter-spacing: 0.5px;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(108, 92, 231, 0.3);
        }

        .toggle-btn {
            background: linear-gradient(135deg, #6C5CE7 0%, #8E7CFF 100%);
        }

        .error {
            color: #ff4757;
            margin-top: 1rem;
            padding: 1rem;
            background: rgba(255, 71, 87, 0.1);
            border-radius: 12px;
            border: 1px solid rgba(255, 71, 87, 0.2);
        }

        .weather-info p {
            margin: 12px 0;
            font-size: 1.1rem;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .city-name {
            font-size: 2rem;
            font-weight: 700;
            margin-bottom: 1.5rem;
            color: var(--text-primary);
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .hourly-container {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
            gap: 1rem;
            margin-top: 1.5rem;
        }

        .hourly-item {
            background: rgba(255,255,255,0.9);
            border-radius: 16px;
            padding: 1.2rem;
            text-align: center;
            box-shadow: 0 4px 6px rgba(0,0,0,0.05);
            transition: transform 0.3s ease;
        }

        .hourly-item:hover {
            transform: translateY(-3px);
        }

        .hourly-time {
            font-weight: 600;
            margin-bottom: 8px;
            color: var(--primary);
        }

        .hourly-temp {
            font-size: 1.4rem;
            font-weight: 700;
            color: var(--secondary);
            margin: 8px 0;
        }

        .weather-icon {
            width: 48px;
            height: 48px;
            margin: 0 auto;
        }

        @media (max-width: 640px) {
            .form-group {
                flex-direction: column;
            }
            
            button {
                justify-content: center;
            }
            
            .hourly-container {
                grid-template-columns: repeat(2, 1fr);
            }
        }

        .detail-quote {
            margin-top: 1.5rem;
            padding: 1rem;
            background: rgba(74, 144, 226, 0.1);
            border-radius: 12px;
            font-style: italic;
            color: #4A90E2;
        }
    </style>
</head>
<body>
    <h1 style="text-align: center; margin-bottom: 2rem; color: var(--text-primary);">
        <i class="fas fa-cloud-sun"></i> 天气精灵
    </h1>
    <form id="cityForm">
        <div class="form-group">
            <input type="text" id="city" name="city" placeholder="输入城市名称，例如：北京" required>
            <button type="submit">
                <i class="fas fa-search"></i>
                <span id="submitText">查询实时天气</span>
            </button>
            <button type="button" id="hourlyBtn" class="toggle-btn">
                <i class="fas fa-clock"></i>
                <span>每小时预报</span>
            </button>
        </div>
    </form>
    <div id="result"></div>

    <script>
        let isHourly = false;
        const hourlyBtn = document.getElementById('hourlyBtn');
        const submitText = document.getElementById('submitText');

        hourlyBtn.addEventListener('click', () => {
            isHourly = !isHourly;
            hourlyBtn.innerHTML = isHourly 
                ? '<i class="fas fa-sun"></i><span>实时天气</span>'
                : '<i class="fas fa-clock"></i><span>每小时</span>';
            submitText.textContent = isHourly ? '查询每小时预报' : '查询实时天气';
        });

        document.getElementById('cityForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const cityInput = document.getElementById('city');
            const resultDiv = document.getElementById('result');
            resultDiv.innerHTML = `
                <div class="weather-card" style="text-align: center;">
                    <i class="fas fa-spinner fa-spin" style="font-size: 2rem;"></i>
                    <p style="margin-top: 1rem;">正在获取天气数据...</p>
                </div>
            `;

            try {
                const response = await fetch('/weather', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: `city=${encodeURIComponent(cityInput.value)}&hourly=${isHourly}`
                });

                const data = await response.json();
                
                if (data.error) {
                    resultDiv.innerHTML = `
                        <div class="weather-card error">
                            <i class="fas fa-exclamation-triangle"></i>
                            <p style="margin-top: 8px;">${data.error}</p>
                        </div>
                    `;
                } else if (data.is_hourly) {
                    const weatherIcons = {
                        '晴': 'sun',
                        '阴': 'cloud',
                        '雨': 'cloud-rain',
                        '雪': 'snowflake'
                    };

                    let hourlyHTML = data.hourly.map(hour => `
                        <div class="hourly-item">
                            <div class="hourly-time">${hour.time}</div>
                            <i class="fas fa-${weatherIcons[hour.weather] || 'cloud'} weather-icon"></i>
                            <div class="hourly-temp">${hour.temp}℃</div>
                            <div style="color: #666; font-size: 0.9rem;">
                                <div><i class="fas fa-tint"></i> ${hour.humidity}%</div>
                                <div><i class="fas fa-wind"></i> ${hour.wind_speed}级</div>
                            </div>
                        </div>
                    `).join('');
                    
                    resultDiv.innerHTML = `
                        <div class="weather-card">
                            <div class="city-name">
                                <i class="fas fa-city"></i>
                                ${data.city} 24小时预报
                            </div>
                            <div class="hourly-container">${hourlyHTML}</div>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="weather-card">
                            <div class="city-name">
                                <i class="fas fa-map-marker-alt"></i>
                                ${data.city} 实时天气
                            </div>
                            <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 1rem;">
                                <div style="font-size: 3rem; color: var(--secondary); text-align: center;">
                                    ${data.temperature}℃
                                </div>
                                <div>
                                    <p><i class="fas fa-cloud"></i> ${data.description}</p>
                                    <p><i class="fas fa-tint"></i> 湿度 ${data.humidity}%</p>
                                    <p><i class="fas fa-wind"></i> 风速 ${data.wind_speed}级</p>
                                </div>
                            </div>
                            <div class="detail-quote">
                                <i class="fas fa-quote-left"></i> ${data.detail}
                            </div>
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="weather-card error">
                        <i class="fas fa-unlink"></i>
                        <p style="margin-top: 8px;">服务暂时不可用，请稍后再试</p>
                    </div>
                `;
                console.error("请求出错:", error);
            }
        });
    </script>
</body>
</html>